//
//  UIControl+LBEvent.m
//  TEXT
//
//  Created by mac on 2025/3/2.
//  Copyright © 2025 刘博. All rights reserved.
//

#import "UIControl+LBEvent.h"
#import "NSObject+Swizzle.h"


static const void *monitorActionNameKey = "monitorActionNameKey";
static const void *KSenderAction = "KSenderAction";
static const void *KTabButtonId = "KTabButtonId";
static const void *KClickActionCounts = "KClickActionCounts";
static const void *skipTrack= "skipTrack";

@implementation UIControl (LBEvent)

@dynamic actionName;

+ (void)load
{
    [NSObject exchangeInstanceMethod:[self class] originalSel:@selector(addTarget:action:forControlEvents:) swizzledSel:@selector(autotracker_addCagegoryTarget:action:forControlEvents:)];
    [NSObject exchangeInstanceMethod:[self class] originalSel:@selector(removeTarget:action:forControlEvents:) swizzledSel:@selector(autotracker_removeCagegoryTarget:action:forControlEvents:)];
}

- (void)autotracker_addCagegoryTarget:(id)target
                               action:(SEL)action
                     forControlEvents:(UIControlEvents)controlEvents
{
    // 自动化埋点：只针对”点击“进行埋点，一次点击一次上报，且先埋点后业务。
    if ([self isKindOfClass:[UITextField class]] == NO && controlEvents == UIControlEventTouchUpInside) {
        // 避免：业务添加多次点击回调时，触发多次埋点或者点击处理顺序错乱。
        NSNumber *hookClickMethod = objc_getAssociatedObject(self, "uep_autotrack_click");
        NSLog(@"哈哈哈哈哈这里的hookClickMethod%@ %@", self, hookClickMethod);
        if (!hookClickMethod) {
            objc_setAssociatedObject(self, "uep_autotrack_click", @(1), OBJC_ASSOCIATION_RETAIN);
            [self setSenderAction:NSStringFromSelector(action)];
            [self autotracker_addCagegoryTarget:self
                                         action:@selector(autotracker_monitorAction:forEvent:)
                               forControlEvents:controlEvents];
        }
        
        // 记录控件绑定点击处理次数，当次数大于0时，进行点击埋点上报。
        NSArray *actionNames = [self actionsForTarget:target forControlEvent:controlEvents];
        NSLog(@"哈哈哈哈哈这里这里的数量数量%@ %@", actionNames, NSStringFromSelector(action));
        if (actionNames.count == 0 || ![actionNames containsObject:NSStringFromSelector(action)]) {
            NSLog(@"这里的原始数量%ld %@", [self autotracker_clickActionCounts], self);
            [self setAutotracker_clickActionCounts:[self autotracker_clickActionCounts] + 1];
        }
    }
    
    [self autotracker_addCagegoryTarget:target
                                 action:action
                       forControlEvents:controlEvents];
}

- (void)autotracker_removeCagegoryTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    // 减少控件绑定点击处理次数，当次数大于0时，进行点击埋点上报。
    NSArray *actionNames = [self actionsForTarget:target forControlEvent:controlEvents];
    if (actionNames.count > 0 && [actionNames containsObject:NSStringFromSelector(action)]) {
        [self setAutotracker_clickActionCounts:[self autotracker_clickActionCounts] - 1];
    }
    [self autotracker_removeCagegoryTarget:target action:action forControlEvents:controlEvents];
}

- (void)autotracker_monitorAction:(UIControl *)sender forEvent:(UIEvent *)event
{
    NSLog(@"哈哈哈执行埋点操作了执行埋点操作了");
    if (self.skipTrack || 0 == [self autotracker_clickActionCounts]) {
        return;
    }
    
    [self autotracker_parseClickPoint:event];
   }

- (NSString *)actionName {
    return objc_getAssociatedObject(self, monitorActionNameKey);
}

- (void)setActionName:(NSString *)monitorActionName{
    objc_setAssociatedObject(self, monitorActionNameKey,
                             monitorActionName,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)senderAction {
    return objc_getAssociatedObject(self, KSenderAction);
}

- (void)setSenderAction:(NSString *)senderAction{
    objc_setAssociatedObject(self, KSenderAction,
                             senderAction,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)tabButtonId {
    return objc_getAssociatedObject(self, KTabButtonId);
}

- (void)setTabButtonId:(NSString *)tabButtonId {
    objc_setAssociatedObject(self, KTabButtonId, tabButtonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSInteger)autotracker_clickActionCounts
{
    NSNumber *counts = objc_getAssociatedObject(self, KClickActionCounts);
    return [counts integerValue];
}

- (void)setAutotracker_clickActionCounts:(NSInteger)count
{
    NSLog(@"这里的数量这里的数量%ld", count);
    objc_setAssociatedObject(self, KClickActionCounts, @(count), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (void)autotracker_parseClickPoint:(UIEvent *)event
{
    // 获取点击位置坐标
    CGPoint clickPoint;
    UITouch *touch = [event touchesForView:self].anyObject;
    if (touch) {
        clickPoint = [touch locationInView:self];
    }

}

- (void)setSkipTrack:(BOOL)skipTrack
{
    objc_setAssociatedObject(self, &skipTrack, @(skipTrack), OBJC_ASSOCIATION_RETAIN);
}

- (BOOL)skipTrack
{
    id value = objc_getAssociatedObject(self, &skipTrack);
    if ([value isKindOfClass:[NSNumber class]]) {
        return [value boolValue];
    }
    return NO;
}

@end
